package com.bjpowernode.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bjpowernode.DlykServerApplication;
import com.bjpowernode.constant.DicEnum;
import com.bjpowernode.entity.TDicValue;

import java.util.List;

/**
 * 称呼转换器
 */
public class AppellationConverter implements Converter<Integer> {

    /**
     * 根据读取的称呼转成对应的主键
     * @param cellData
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        //获取excel中的称呼
        String appellation = cellData.getStringValue();

        //从缓存map中获取称呼的集合
        List<TDicValue> tDicValueList = (List<TDicValue>)DlykServerApplication.cacheMap.get(DicEnum.APPELLATION.getCode());

        for (TDicValue tDicValue : tDicValueList) {
            Integer id = tDicValue.getId();
            String typeValue = tDicValue.getTypeValue();

            //excel中填写的称呼与缓存中的比对
            if (appellation.equals(typeValue)) {
                return id;
            }
        }

        return tDicValueList.get(0).getId();
    }
}
